$('.select2').select2();

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

var currActiveId = 0;
$("#active_select").change(function () {
    currActiveId = $("#active_select").val();
});

currActiveId = GetQueryString('Index');
getData(1);
$('.search_btn1').click(function () {
    getData(1);
})

function getData(curr) {
    // var kind_text = $(".kind_sel").select2('val');
    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/ActivityDetails",
        async: true,
        data: {
            // Content:kind_text,
            activeId: currActiveId,
            page: curr,
            row: 10
        },
        success: function (data) {
            if (data[0].length == 0) {
                layer.msg('暂无数据');
            }
            pageDivd(curr, data[1]);
            TableView(data[0]);
            AddActiveOptions(data[1]);
            AddProductOptions(data[2]);
            $('.all_data').text(data[0].length)
        }
    });
}

function pageDivd(curr, page_num) {
    layui.use(['laypage', 'layer'], function () {
        var laypage = layui.laypage
            , layer = layui.layer;
        laypage.render({
            elem: 'demo7'
            , count: page_num
            , limit: 10
            , curr: curr
            , jump: function (obj, first) {
                if (!first) {
                    getData(obj.curr);

                }
            }
        });
    })
}

function AddActiveOptions(data) {
    var box = $('#active_select');
    box.empty();
    for (var i = 0; i < data.length; i++) {
        var text = `<option value='${data[i].activity_id}'>${data[i].activity_name}</option>`;
        box.append(text);
    }
    $("#active_select  option[value='" + currActiveId + "']").attr("selected", true);
}

function AddProductOptions(data) {
    var box = $('#act_product');
    box.empty();
    for (var i = 0; i < data.length; i++) {
        var text = `<option value='${data[i].product_id}'>${data[i].product_name}</option>`;
        box.append(text);
    }
}

//排序
function kindChange(num) {
    //互换
    var kind_text = $(".kind_sel").select2('val');

    $('.order').click(function () {
        var kindLabel = $(this).attr('label');
        $.ajax({
            type: "get",
            url: "https://www.hgxrshn.com/MgProductSorted",
            async: true,
            data: {
                Id: num,
                Kind: kindLabel,
                Content: kind_text
            },
            success: function (data) {
                if (data == 'success') {
                    layer.msg('排序成功', {time: 500}, function () {
                        location.reload();
                    })
                } else {
                    layer.msg('排序失败', {time: 500})
                }
            }
        });
    })

    $('.order5').click(function () {
        var firseCheck, secondCheck;
        var check = $('input[type=checkbox]:checked');
        if (check.length == 2) {
            firseCheck = check.eq(0).attr('index');
            secondCheck = check.eq(1).attr('index');
            $.ajax({
                type: "get",
                url: "https://www.hgxrshn.com/MgProductSwap",
                async: true,
                data: {
                    Content: kind_text,
                    Behind: secondCheck,
                    First: firseCheck,

                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('交换成功', {time: 500}, function () {
                            location.reload();
                        })
                    } else {
                        layer.msg('交换失败', {time: 500})
                    }

                }
            });
        } else {
            layer.msg('请选中两个商品')
        }
    })


}

function TableView(data) {
    $('#act_detail_table').html('');
    for (var i = 0; i < data.length; i++) {
        // var radio1,radio2,radio3;
        // if(data[i][6].indexOf('新品')>-1){
        // 	radio1 = '<input type="radio" checked="checked" disabled="disabled">';
        // }else{
        // 	radio1 = '<input type="radio" disabled="disabled">';
        // }
        // if(data[i][6].indexOf('热卖')>-1){
        // 	radio2 = '<input type="radio" checked="checked" disabled="disabled">';
        // }else{
        // 	radio2 = '<input type="radio" disabled="disabled">';
        // }
        // if(data[i][6].indexOf('推荐')>-1){
        // 	radio3 = '<input type="radio" checked="checked" disabled="disabled">';
        // }else{
        // 	radio3 = '<input type="radio" disabled="disabled">';
        // }

        var trText = `<tr>
		      		    		<td>${data[i][0]}</td>
		      		    		<td>${data[i][1]}</td>
		      		    		<td>
		      		    			<img src="${data[i][2]}" height="50"/>
		      		    		</td>
		      		    		<td>
		      		    			${data[i][3]}
		      		    		</td>
		      		    		<td>
		      		    			￥${data[i][4]}
		      		    		</td>
		      		    	
		      		    		<td index="${data[i][0]}" style="width: 120px;">
		      		    			<div class="btn_done">
		      		    				<a class="look_detail" href="#">查看</a>
		      		    	
		      		    			</div>
		      		    			<div>
		      		    				<a class="kind_del" href="#">此活动下删除商品</a>
		      		    			</div>
		      		    		</td>
		      		    	</tr>`;

        $('#act_detail_table').append(trText);

    }
    data_edtil();

}

function data_edtil() {
    $("#check_all").click(function () {
        if ($(this).prop('checked') == true) {

            $("tbody").find('input[type=checkbox]').prop('checked', 'checked')
        } else {
            $("tbody").find('input[type=checkbox]').prop('checked', false)
        }
    })
    $('.order_by').click(function (ev) {
        var Number = $(this).parent().parent().attr('index');
        var ev = ev || event;
        $('.check_do').css('display', 'block')
        $('.check_do').css('left', ev.clientX + 'px');
        $('.check_do').css('top', ev.clientY + 'px');

        kindChange(Number);
    })
    $('.look_detail').click(function () {
        var Index = $(this).parent().parent().attr('index');
        window.location.href = "http://www.puer.zone:9000/product_details.html?Index=" + Index;
    })
    $('.check_do').mouseleave(function () {
        $('.check_do').css('display', 'none');
    })
    $('.kind_del').click(function () {
        // var kind_text = $(".kind_sel").select2('val');
        // var checkNode = $("tbody").find('input[type=checkbox]:checked');
        // var checkArr=[];
        // var eventid = $(this).parent().parent().attr('index');
        // if(checkNode.length>0){
        // 	for (var i = 0;i<checkNode.length;i++) {
        // 		var eventId = checkNode.eq(i).attr('index');
        // 		checkArr.push(eventId);
        // 	}
        // }else{
        // 	checkArr.push(eventid);
        // }
        var product = $(this).parent().parent().attr('index');
        layer.confirm('确定要在此活动下删除此商品吗？', {
            title: ['提示'],
            btn: ['确定', '取消'] //按钮
        }, function () {

            $.ajax({
                type: "get",
                url: "http://www.puer.zone:30000/ActivityProductDel",
                async: true,
                data: {
                    productId: product,
                    activeId: currActiveId
                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('删除成功', {time: 500}, function () {
                            getData(1);
                        })
                    } else {
                        layer.msg('删除失败', {time: 500})
                    }
                }
            });
        }, function () {
        });
    })
}

$("#add-pro-act").click(function () {
    var product = $("#act_product").val();
    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/ActivityProductAdd",
        async: true,
        data: {
            productId: product,
            activityId: currActiveId
        },
        success: function (data) {
            if (data == 'success') {
                layer.msg('添加成功', {time: 500}, function () {
                    getData(1);
                })
            } else {
                layer.msg('添加失败', {time: 500})
            }
        }
    });
})